<?php
/**
 * 尚善福· 会员订单模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2022 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com；
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Home\Controller;
class OrderController extends BaseController {
	/**
	 +--------------------------------------------------------------------------
	 * 实现获取全部订单列表的功能
	 * @author:GH
     * @date: 2018-12-26
	 +--------------------------------------------------------------------------
	 */
    public function orderList(){
        $this->assign('type',I('get.type','1'));
        $this->display();
    }
    public function ajaxOrderList(){
        $usid = session('user_auth.usid');
        $type = trim(I('post.type'));
        $page = trim(I('p/d',2));
        //echo $type;
        //实例化相关表
        $orident = M('order');
        $deident = M('order_details');
        // 搜索条件
        $condition = array();
        $condition['or_uid'] = $usid;
        $condition['or_type'] ='1';
        if($type=='1'){
            $condition['or_ostate'] = array('neq','3');
        }elseif($type=='2'){
            $condition['or_ostate'] = '1';
            $condition['or_pstate'] = '1';
        }elseif($type=='3'){
            $condition['or_ostate'] = '1';
            $condition['or_pstate'] = '2';
            $condition['or_wstate'] = '1';
        }elseif($type=='4'){
            $condition['or_ostate'] = '1';
            $condition['or_pstate'] = '2';
            $condition['or_wstate'] = '2';
        }
        //dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;die;
        $count = $orident->where($condition)->count();
        $num  = 30;//每页记录数
        $limit= ($page-1)*$num;//每次查询取记录
        $list = $orident->where($condition)->order($sort_order)->limit($limit,$num)->
        field('or_id,or_batch,or_xmon,or_zmon,or_jmon,or_fmon,or_ostate,or_pstate,or_wstate,or_expr,or_xtime')->select();
        //dump($list);die;
        foreach($list as $key=>$val){
            $list[$key]['expre'] = unserialize($val['or_expr']);
            $list[$key]['goods'] = $deident->where("`de_oid`='".$val['or_id']."' and `de_uid`='".$usid."'")->
            field('de_gid,de_gname,de_gnum,de_gmoney,de_jmoney,de_ymoney,de_spec,de_gprice')->select();
        }
        //echo $orident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$page+1);
        $this->assign('type',$type);
        $this->assign('path',"http://".$_SERVER["SERVER_NAME"]."/Home/Order/orderList/type/".$type);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现会员确认收货的功能
     * @author:GH
     * @date: 2018-12-26
     +--------------------------------------------------------------------------
     */
    public function takeOrder(){
        $usid = session('user_auth.usid');
        $orid = trim(I('post.id/d'));
        //实例化相关表
        $orident = M('order');
        $usident = M('user');
        $acident = M('account');
        $conf = "`or_id`='".$orid."' and `or_uid`='".$usid."'";  
        //获取订单记录
        $orFind = $orident->where($conf)->field('or_id,or_wstate,or_zmon')->find();
        if(count($orFind)=='0'){
            $res= array('status'=>-1,'msg'=>'参数错误'); // 返回结果状态
            exit(json_encode($res));
        }
        //判断订单状态：如果该订单不是未支付状态，不可删除
        if('2'!=$orFind['or_wstate']){
            $res= array('status'=>-1,'msg'=>'该订单不可确认收货'); // 返回结果状态
            exit(json_encode($res));
        }
        //更改 订单信 息
        $data['or_wstate']= '3';
        $data['or_qtime'] = time();
        $where = "$conf and `or_pstate`=2 and `or_wstate`=2";
        $ore = $orident->where($where)->save($data);
        //记录订单日志
        logOrder($orid,'2','用户确认收货','确认收货');
        if($ore){
            //执行会员消费积分释放的功能
            order_shifang($usid,$orFind['or_zmon']);
            $res= array('status'=>1,'msg'=>'确认收货成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'确认收货失败'); // 返回结果状态
        }
        exit(json_encode($res));
    }
    /**
     +--------------------------------------------------------------------------
     * 实现取消订单记录的功能
     * @author:GH
     * @date: 2018-12-26
     +--------------------------------------------------------------------------
     */
    public function cancelOrder(){
        $usid = session('user_auth.usid');
        $orid = trim(I('post.id/d'));
        //实例化相关表
        $usident = M('user');
        $orident = M('order');
        $acident = M('account');
        $conf = "`or_id`='".$orid."' and `or_uid`='".$usid."'";
        //获取订单记录
        $orFind = $orident->where($conf)->field('or_id,or_pstate,or_batch')->find();
        if(count($orFind)=='0'){
            $res= array('status'=>-1,'msg'=>'参数错误'); // 返回结果状态
            exit(json_encode($res));
        }
        //判断订单状态：如果该订单不是未支付状态，不可删除
        if('1'!=$orFind['or_pstate']){
            $res= array('status'=>-1,'msg'=>'该订单不可取消'); // 返回结果状态
            exit(json_encode($res));
        }
        //判断该订单是否支付余额或积分，若存在，则退还金额
        $acFind = $acident->where("`ac_oid`='".$orid."' and `ac_uid`='".$usid."' and `ac_batch`='".$orFind['or_batch']."'")->find();
        if($acFind['ac_money']!='0' && $acFind['ac_money']!= NULL){
            $data = array(
                'us_money' => ['exp','us_money-'.$acFind['ac_money']],
            );
            $usre = $usident->where("`us_uid`='".$usid."'")->save($data);
        }
        //更改订单状态
        $ore = $orident->where("$conf and `or_pstate`=1")->setField('or_ostate','2');  
        //更改财务记录信息和记录日志
        if($ore>0){
            //$acdata['ac_money'] = '0';
            $acident->where("`ac_id`='".$acFind['ac_id']."'")->setField('ac_money','0'); ; //财务记录
            //记录订单日志
            logOrder($orid,'2','用户取消订单','取消订单');
        }        
        if($ore){
            $res= array('status'=>1,'msg'=>'取消成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'取消失败'); // 返回结果状态
        }
        exit(json_encode($res));
    }
    /**
    +--------------------------------------------------------------------------
     * 实现删除兑换订单记录的功能
     * @author:GH
     * @date: 2018-12-26
    +--------------------------------------------------------------------------
     */
    public function prdelOrder(){
        $usid = session('user_auth.usid');
        $orid = trim(I('post.id/d'));
        //实例化相关表
        $orident = M('prop_order');
        $conf = "`or_id`='".$orid."' and `or_uid`='".$usid."'";
        //获取订单记录
        $orFind = $orident->where($conf)->field('or_id,or_wstate,or_batch')->find();
        if(count($orFind)=='0'){
            $res= array('status'=>-1,'msg'=>'参数错误'); // 返回结果状态
            exit(json_encode($res));
        }
        //删除订单记录
        if($orid>0){
            $orre = $orident->where("$conf")->delete(); //删除订单记录
        }
        if($orre){
            $res= array('status'=>1,'msg'=>'删除成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'删除失败'); // 返回结果状态
        }
        exit(json_encode($res));
    }
    /**
     +--------------------------------------------------------------------------
     * 实现删除订单记录的功能
     * @author:GH
     * @date: 2018-12-26
     +--------------------------------------------------------------------------
     */
    public function delOrder(){
        $usid = session('user_auth.usid');
        $orid = trim(I('post.id/d'));
        //实例化相关表
        $usident = M('user');
        $orident = M('order');
        $deident = M('order_details');
        $reident = M('order_remarks');
        $acident = M('account');
    
        $conf = "`or_id`='".$orid."' and `or_uid`='".$usid."'";
        //获取订单记录
        $orFind = $orident->where($conf)->field('or_id,or_pstate,or_batch')->find();
        if(count($orFind)=='0'){
            $res= array('status'=>-1,'msg'=>'参数错误'); // 返回结果状态
            exit(json_encode($res));
        }
        //判断订单状态：如果该订单不是未支付状态，不可删除
        if('1'!=$orFind['or_pstate']){
            $res= array('status'=>-1,'msg'=>'该订单不可删除'); // 返回结果状态
            exit(json_encode($res));
        }
        //判断该订单是否支付余额或积分，若存在，则退还金额
        $acFind = $acident->where("`ac_oid`='".$orid."' and `ac_uid`='".$usid."' and `ac_batch`='".$orFind['or_batch']."'")->find();
//         if(count($acFind)=='0'){
//             $res= array('status'=>-1,'msg'=>'财务参数错误'); // 返回结果状态
//             exit(json_encode($res));
//         }
        if($acFind['ac_money']!='0' && $acFind['ac_money']!= NULL){
            $data = array(
                'us_money' => ['exp','us_money-'.$acFind['ac_money']],
            );
            $usre = $usident->where("`us_uid`='".$usid."'")->save($data);
        }
        //删除订单记录 
        if($orid>0){
            $orre = $orident->where("$conf and `or_pstate`=1")->delete(); //删除订单记录
            $deident->where("`de_oid`='".$orid."'")->delete();  //订单详细
            $reident->where("`re_oid`='".$orid."'")->delete();  //订单日志
            $acre = $acident->where("`ac_id`='".$acFind['ac_id']."'")->delete(); //删除财务记录
        }
        if($orre){
            $res= array('status'=>1,'msg'=>'删除成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'删除失败'); // 返回结果状态
        }
        exit(json_encode($res));
    }
    /**
     +--------------------------------------------------------------------------
     * 实现订单详情页的功能
     * @author:GH
     * @date: 2018-12-26
     +--------------------------------------------------------------------------
     */
    public function orderDetail(){
        $uid = session('user_auth.usid');
        //实例化相关表
        $usident = M('user');
        $orident = M('order');
        $deident = M('order_details');
        $oid = trim(I('get.oid/d'));
        //获取相关数据
        $deList = $deident->where("`de_oid`='".$oid."' and `de_uid`='".$uid."'")->select();
        //dump($oid);die;
        $orFind = $orident->where("`or_id`='".$oid."' and `or_uid`='".$uid."'")->find();
        $orFind['addre'] = addreSerialize($orFind['or_add'],'2');
        //dump($orFind);die;
        $this->assign('dres',$deList);
        $this->assign('res',$orFind);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取兑换订单列表的功能
     * @author:GH
     * @date: 2018-12-26
     +--------------------------------------------------------------------------
     */
    public function propList(){
        $usid = session('user_auth.usid');
        $type = trim(I('post.type'));
        $page = trim(I('p/d',2));
        //echo $type;
        //实例化相关表
        $orident = M('order');
        $prident = M('prop');
        $poident = M('prop_order');
        // 搜索条件
        $condition = array();
        $condition['or_uid'] = $usid;
        $condition['or_type'] ='2';
        //dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;die;
        $count = $orident->where($condition)->count();
        $num  = 30;//每页记录数
        $limit= ($page-1)*$num;//每次查询取记录
        $list = $poident->where($condition)->select();
        foreach($list as $key=>$val){
            $list[$key]['expre'] = unserialize($val['or_expr']);
            $list[$key]['good'] = $prident->where("`gp_id`='" . $list[$key]['or_goid'] . "'")->find();
        }
        //echo $orident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$page+1);
        $this->assign('type',$type);
        $this->assign('path',"http://".$_SERVER["SERVER_NAME"]."/Home/Order/orderList/type/".$type);
        $this->display();
    }
    
    /**
     +--------------------------------------------------------------------------
     * 实现会员确认收货的功能（兑换订单）
     * @author:GH
     * @date: 2018-12-26
     +--------------------------------------------------------------------------
     */
    public function takePropOrder(){
        $usid = session('user_auth.usid');
        $orid = trim(I('post.id/d'));
        //实例化相关表
        $usident = M('user');
        $orident = M('prop_order');
        $conf = "`or_id`='".$orid."' and `or_uid`='".$usid."'";
        //获取订单记录
        $orFind = $orident->where($conf)->field('or_id,or_wstate')->find();
        if(count($orFind)=='0'){
            $res= array('status'=>-1,'msg'=>'参数错误'); // 返回结果状态
            exit(json_encode($res));
        }
        //判断订单状态：如果该订单不是未支付状态，不可删除
        if('2'!=$orFind['or_wstate']){
            $res= array('status'=>-1,'msg'=>'该订单不可确认收货'); // 返回结果状态
            exit(json_encode($res));
        }
        //更改订单信息
        $data['or_wstate']= '1';
        $where = "$conf and `or_wstate`=2";
        $ore = $orident->where($where)->save($data);
    
        if($ore){
            $res= array('status'=>1,'msg'=>'确认收货成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'确认收货失败'); // 返回结果状态
        }
        exit(json_encode($res));
    }
    /**
    +--------------------------------------------------------------------------
     * 实现获取兑换订单列表的功能
     * @author:GH
     * @date: 2018-12-26
    +--------------------------------------------------------------------------
     */
    public function dailiList(){
        $usid = session('user_auth.usid');
        $type = trim(I('post.type'));
        $page = trim(I('p/d',2));
        //echo $type;
        //实例化相关表
        $orident = M('order');
        $dgident = M('daili_goods');
        $doident = M('daili_order');
        // 搜索条件
        $condition = array();
        $condition['or_uid'] = $usid;
        $condition['or_type'] ='2';
        //dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;die;
        $count = $orident->where($condition)->count();
        $num  = 30;//每页记录数
        $limit= ($page-1)*$num;//每次查询取记录
        $list = $doident->where($condition)->select();

        foreach($list as $key=>$val){
            $list[$key]['expre'] = unserialize($val['or_expr']);
            $list[$key]['good'] = $dgident->where("`dg_id`='" . $list[$key]['do_goid'] . "'")->find();
        }
//        dump($list);
        //echo $orident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$page+1);
        $this->assign('type',$type);
        $this->assign('path',"http://".$_SERVER["SERVER_NAME"]."/Home/Order/orderList/type/".$type);
        $this->display();
    }
}